*******************************************************************************
* > Loop over datasets
*******************************************************************************

foreach dataset in gallup gss {

*******************************************************************************
* Load data
*******************************************************************************

use dataset_`dataset', clear
	
*******************************************************************************
* Label lines if it's the first plot
*******************************************************************************

local mylabel "Police"
if "`dataset'" == "gss" local mylabel "Law Enforcement"

local legend1 legend(off)
if "`subgroup'" == "All" {
	local legend1 legend(order(1 ///
		"{fontface monospace:{bf:`mylabel'}}" ///
		2 "{fontface monospace:Control Series}") ///
		symysize(0) symxsize(0) ring(0) pos(11) cols(1) ///
		region(lwid(none)) fcol(none))	
}

*******************************************************************************
* Set params based on data set
*******************************************************************************

if "`dataset'" == "gallup" {
	rename conf Y
	local mytitle2 "Confidence (Gallup)"
	local treated_target "police"
	local yaxis -.6(.2).6
	local xaxis 1990(8)2022
}
if "`dataset'" == "gss" {
	rename nat Y
	local mytitle2 "Spending (GSS)"
	local treated_target "crimy"
	local yaxis -.6(.2).6
	local xaxis 1982(8)2022	
}

*******************************************************************************
* Make the party differences by opinion object
*******************************************************************************

egen Y_dem = mean(Y) if subgroup == "Democrat", by(target year)
egen Y_rep = mean(Y) if subgroup == "Republican", by(target year)
collapse Y_dem Y_rep, by(target year)
gen Y = Y_dem - Y_rep

*******************************************************************************
* Make lags
*******************************************************************************

su year
local mymin = r(min)

local lags
levelsof year if year < 2014, local(levels)
foreach i of local levels {
	egen Y`i'm = mean(Y) if year == `i', by(target)
	egen Y`i' = mean(Y`i'm), by(target)
	drop Y`i'm
	local lags `lags' Y`i'
}

*******************************************************************************
* >> Loop over placebo series 
*******************************************************************************

encode target, gen(id)
tsset id year
local i = 1
levelsof target, local(levels)
local nlevels = r(r)
foreach l of local levels {
	
	su id if target == "`l'"
	local treated = r(mean)

*******************************************************************************
* Do the synth
*******************************************************************************

	#delimit;

	synth 
		Y `lags',
			trunit(`treated')
			trperiod(2014)   
			keep(tmp_`i', replace)
			;

	#delimit cr
		
	preserve
		use tmp_`i', clear
		gen target = "`l'"
		save tmp_`i', replace
	restore

	local i = `i' + 1

*******************************************************************************
* >> Close loop over placebo series
*******************************************************************************	
	
}

*******************************************************************************
* Graph all placebo series
*******************************************************************************

use tmp_1, clear
erase tmp_1.dta
forvalues i = 2(1)`nlevels' {
	append using tmp_`i'
	erase tmp_`i'.dta
}

gen D = _Y_treat - _Y_syn
sort target _time
encode target, gen(targetid)

levelsof targetid, local(levels)
foreach l of local levels {
	local p (line D _time if targetid == `l', col(gray))
	local placebos `placebos' `p'
}

#delimit;

gr tw
	`placebos'
	(line D _time if target == "`treated_target'", col(black) lwid(thick))
	,
		`legend1'
		xline(2014, lpat(dash))
		yline(0, lpat(shortdash))
		name(g`dataset'1, replace)
		plotregion(style(none))
		ylab(`yaxis', angle(horiz))
		ytitle("")
		xtitle("")
		xlab(`xaxis')
		;

#delimit cr

gr_edit .legend.plotregion1.label[2].style.editstyle color(gray) editcopy

*******************************************************************************
* Graph RMSPE (one-sided test)
*******************************************************************************

gen DD = D
if "`subgroup'" != "Republican" replace DD = 0 if DD > 0 & _time >= 2014
if "`subgroup'" == "Republican" replace DD = 0 if DD < 0 & _time >= 2014
gen pre_mspem = DD^2 if _time < 2014
egen pre_mspe = mean(pre_mspem), by(target)
gen post_mspem = DD^2 if _time >= 2014
egen post_mspe = mean(post_mspem), by(target)

gen ratio_mspe = sqrt(post_mspe) / sqrt(pre_mspe)

collapse ratio post_mspe pre_mspe, by(target)
sort ratio
gen index = _n

if "`dataset'" == "gallup" {
	replace target = subinstr(target, "_", " ", 1)
	replace target = proper(target)
	replace target = "Supreme Court" if target == "Sup Ct"
	replace target = "TV News" if target == "Tv News"
}

if "`dataset'" == "gss" {
	replace target = "Foreign aid" if target == "aid"
	replace target = "Foreign aid alt" if target == "aidy"
	replace target = "Military" if target == "arms"
	replace target = "Military alt" if target == "armsy"
	replace target = "Cities" if target == "city"
	replace target = "Cities alt" if target == "cityy"
	replace target = "Crime" if target == "crime"
	replace target = "Law enforcement" if target == "crimy"
	replace target = "Drugs" if target == "drug"
	replace target = "Drugs alt" if target == "drugy"
	replace target = "Education" if target == "educ"
	replace target = "Education alt" if target == "educy"
	replace target = "Environment" if target == "envir"
	replace target = "Environment alt" if target == "enviy"
	replace target = "Welfare" if target == "fare"
	replace target = "Welfare alt" if target == "farey"
	replace target = "Health" if target == "heal"
	replace target = "Health alt" if target == "healy"
	replace target = "Transportation" if target == "mass"
	replace target = "Parks and rec" if target == "park"
	replace target = "Blacks" if target == "race"
	replace target = "Blacks alt" if target == "racey"
	replace target = "Roads" if target == "road"
	replace target = "Social Security" if target == "soc"
	replace target = "Space" if target == "spac"
	replace target = "Space alt" if target == "spacy"
}

replace target = target + (15-length(target))*" "
replace target = "{fontface monospace:" + target + "}"

labmask index, values(target)

#delimit;

gr tw
	(sc index ratio if regex(target, "Law enforcement") | 
		regex(target, "Police"), 
		col(black) msym(O))
	(sc index ratio if !regex(target, "Law enforcement") & 
		!regex(target, "Police"), 
		col(black) msym(Oh))
	, 
		name(g`dataset'2, replace)
		ylab(1(1)`nlevels', angle(horiz) valuelabel labsize(vsmall))
		plotregion(style(none))
		ytitle("")
		xtitle("Postperiod RMSPE / Preperiod RMSPE", size(small))
		legend(off)
		;
		
#delimit cr

*******************************************************************************
* Combine plots within datasets
*******************************************************************************

gr combine g`dataset'1 g`dataset'2, name(G`dataset', replace) ///
	title("`mytitle2'")

*******************************************************************************
* > Close dataset loop
*******************************************************************************

}

*******************************************************************************
* Combine plots for each dataset
*******************************************************************************

local mytitle `subgroup'
if "`subgroup'" != "All" local mytitle = "`subgroup's" 

gr combine Ggallup Ggss, cols(1) ysize(4) xsize(5) imargin(zero)

*******************************************************************************
* Save
*******************************************************************************

gr export "figure_sc_placebos_partydiffs.pdf", replace

*******************************************************************************
* End
*******************************************************************************
